package com.ruoyi.game.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.game.domain.entity.GamePick3Record;
import com.ruoyi.game.mapper.GamePick3RecordMapper;
import com.ruoyi.game.service.IGamePick3RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * pick3开奖记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class GamePick3RecordServiceImpl implements IGamePick3RecordService
{
    @Autowired
    private GamePick3RecordMapper gamePick3RecordMapper;

    /**
     * 查询pick3开奖记录
     * 
     * @param id pick3开奖记录主键
     * @return pick3开奖记录
     */
    @Override
    public GamePick3Record selectGamePick3RecordById(String id)
    {
        return gamePick3RecordMapper.selectGamePick3RecordById(id);
    }

    /**
     * 查询pick3开奖记录列表
     * 
     * @param gamePick3Record pick3开奖记录
     * @return pick3开奖记录
     */
    @Override
    public List<GamePick3Record> selectGamePick3RecordList(GamePick3Record gamePick3Record)
    {
        return gamePick3RecordMapper.selectGamePick3RecordList(gamePick3Record);
    }

    /**
     * 新增pick3开奖记录
     * 
     * @param gamePick3Record pick3开奖记录
     * @return 结果
     */
    @Override
    public int insertGamePick3Record(GamePick3Record gamePick3Record)
    {
        gamePick3Record.setCreateTime(DateUtils.getNowDate());
        return gamePick3RecordMapper.insertGamePick3Record(gamePick3Record);
    }

    /**
     * 修改pick3开奖记录
     * 
     * @param gamePick3Record pick3开奖记录
     * @return 结果
     */
    @Override
    public int updateGamePick3Record(GamePick3Record gamePick3Record)
    {
        return gamePick3RecordMapper.updateGamePick3Record(gamePick3Record);
    }

    /**
     * 批量删除pick3开奖记录
     * 
     * @param ids 需要删除的pick3开奖记录主键
     * @return 结果
     */
    @Override
    public int deleteGamePick3RecordByIds(String[] ids)
    {
        return gamePick3RecordMapper.deleteGamePick3RecordByIds(ids);
    }

    /**
     * 删除pick3开奖记录信息
     * 
     * @param id pick3开奖记录主键
     * @return 结果
     */
    @Override
    public int deleteGamePick3RecordById(String id)
    {
        return gamePick3RecordMapper.deleteGamePick3RecordById(id);
    }

    @Override
    public List<GamePick3Record> page(int pageNo, int pageSize, int timeLimit) {
        PageHelper.offsetPage(pageNo,pageSize,Boolean.FALSE);
        return  gamePick3RecordMapper.page(timeLimit);
    }

    @Override
    public GamePick3Record findByRoundNo(String roundNo) {
        GamePick3Record gamePick3Record = new GamePick3Record();
        gamePick3Record.setRoundNo(roundNo);
        List<GamePick3Record> list = gamePick3RecordMapper.selectGamePick3RecordList(gamePick3Record);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean updateLottery(String roundNo, int[] balls) {
        GamePick3Record gamePick3Record = findByRoundNo(roundNo);
        if(ObjectUtil.isNull(gamePick3Record)){
            return false;
        }
        if(gamePick3Record.getToEnd() == Constants.NUM_ONE){
            return false;
        }
        GamePick3Record update = new GamePick3Record();
        update.setId(gamePick3Record.getId());
        update.setaBallNo(balls[0]+"");
        update.setbBallNo(balls[1]+"");
        update.setcBallNo(balls[2]+"");
        update.setToEnd(Constants.NUM_ONE);
        return gamePick3RecordMapper.updateGamePick3Record(update) > 0;
    }

    @Override
    public void saveBatch(List<GamePick3Record> list) {
        String roundNo = list.get(0).getRoundNo();
        GamePick3Record gamePick3Record = findByRoundNo(roundNo);
        if(ObjectUtil.isNotNull(gamePick3Record)){
            return;
        }
        for(GamePick3Record record : list){
            gamePick3RecordMapper.insertGamePick3Record(record);
        }
    }
}
